const usersModel = require('../util/models/users');
const videoModel = require('../util/models/video');
const commentModel = require('../util/models/comment');
const danmakuModel = require('../util/models/danmaku');

const Constant = require('../util/constant');
const Common = require('../util/common')

let exportObj = {
    changeLike,
    changeUserLike,
    addComment,
    addDanmaku
    // changeCoin,
    // changeStar,
    // changeShare,
};

module.exports = exportObj;

function changeLike(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    var like
    let tasks = {
        query: cb => {
            videoModel
                .findOne({ where: { id: req.body.id } })
                .then(function (result) {
                    like = result.likes
                    if (req.body.isAdd) {
                        like++
                    } else {
                        like--
                    }
                    cb(null);
                })
                .catch(function (err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        },
        update: ['query', (result, cb) => {
            videoModel
                .update({ likes: like }, { where: { id: req.body.id } })
                .then(function (result) {
                    resObj.data = like
                    cb(null);
                })
                .catch(function (err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }]
    };
    Common.autoFn(tasks, res, resObj)
}

function changeUserLike(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        update: cb => {
            usersModel
                .update({ like: req.body.arrLike }, { where: { id: req.body.id } })
                .then(function (result) {
                    resObj.data = result
                    cb(null);
                })
                .catch(function (err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }
    };
    Common.autoFn(tasks, res, resObj)
}

function changeUserLike(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        update: cb => {
            usersModel
                .update({ like: req.body.arrLike }, { where: { id: req.body.id } })
                .then(function (result) {
                    resObj.data = result
                    cb(null);
                })
                .catch(function (err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }
    };
    Common.autoFn(tasks, res, resObj)
}

function addComment(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        //增加commentModel
        add: cb => {
            commentModel
                .create({
                    video_id: req.body.video_id,
                    user_id: req.body.user_id,
                    talk: req.body.talk
                })
                .then(function (result) {
                    resObj.data = result
                    cb(null);
                })
                .catch(function (err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        },
    };
    Common.autoFn(tasks, res, resObj)
}

function addDanmaku(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        //增加danmakuModel
        add: cb => {
            danmakuModel
                .create({
                    video_id: req.body.video_id,
                    user_id: req.body.user_id,
                    time: req.body.time,
                    talk: req.body.talk
                })
                .then(function (result) {
                    resObj.data = result
                    cb(null);
                })
                .catch(function (err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        },
    };
    Common.autoFn(tasks, res, resObj)
}